Passed
Push — master ( d20af7...0795ac )
by lv
02:52 queued 01:02
created

io.sockets.connection   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
dl 0
loc 11
rs 10
nop 0
1
const Koa = require('koa')
2
const app = new Koa()
3
const server = require('http').createServer(app.callback())
4
const io = require('socket.io').listen(server)
5
6
console.log('here')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
7
8
const users = {} // 存储在线用户列表的对象
9
10
// io.on('connection', function (socket) {
11
//     socket.emit('news', { hello: 'world' })
12
//     socket.on('my other event', function (data) {
13
//         console.log(data)
14
//     })
15
//     socket.emit('news', { hello: 'very good2' })
16
// })
17
18
io.sockets.on('connection', function (socket) {
19
20
    //有人上线
21
    socket.on('online', function (data) {
22
        console.log('online data: ', data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
23
        //将上线的用户名存储为 socket 对象的属性,以区分每个 socket 对象,方便后面使用
24
        socket.name = data.user
25
        //users 对象中不存在该用户名则插入该用户名
26
        if (!users[data.user]) {
27
            users[data.user] = data.user
28
        }
29
        //向所有用户广播该用户上线信息
30
        io.sockets.emit('online', { users: users, user: data.user })
31
    })
32
33
    //有人发话
34
    socket.on('say', function (data) {
35
        console.log('say data: ', data)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
36
        if (data.to == 'all') {
37
            //向其他所有用户广播该用户发话信息
38
            socket.broadcast.emit('say', data)
39
        } else {
40
            //向特定用户发送该用户发话信息
41
            //clients 为存储所有连接对象的数组
42
            var clients = io.sockets.clients()
43
            console.log('-- clients info: ---', clients)
44
            //遍历找到该用户
45
            clients.forEach(function (client) {
46
                if (client.name == data.to) {
47
                    //触发该用户客户端的 say 事件
48
                    client.emit('say', data)
49
                }
50
            })
51
        }
52
    })
53
54
    //有人下线
55
    socket.on('disconnect', function () {
56
        console.log(socket.name + ' is disconnect')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
57
        //若 users 对象中保存了该用户名
58
        if (users[socket.name]) {
59
            //从 users 对象中删除该用户名
60
            delete users[socket.name]
61
            console.log('still online: ', users)
62
            //向其他所有用户广播该用户下线信息
63
            socket.broadcast.emit('offline', { users: users, user: socket.name })
64
        }
65
    })
66
})
67
68
server.listen(3000, function () {
69
    console.log('socket server listening on port 3000')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
70
})